package components.plugins.mlp
{
	import MLP.multiLayerObject;
	
	import UIFree.uif_component;
	
	import UIUtils.uif_MLPEncoder;
	
	import components.clt_component;
	
	import flash.display.Bitmap;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class clt_mlpImage extends clt_component {
		private var _image		: multiLayerObject;
		private var _mask		: clt_component;
		private var _backSize	: Point;
		public function clt_mlpImage(a_parent: uif_component, bitmaps: Array=null, bounds: Rectangle=null, styleClass:String='') {
			if (!bounds) bounds = new Rectangle();
			super(a_parent, bounds.x, bounds.y, bounds.width, bounds.height, styleClass);
			_mask = new clt_component(this, 0, 0, width, height, 'white');
			
			if (bitmaps) assign(bitmaps);
		}
		
		public function assign(bitmaps: Array, rescale: Boolean=false): void {
			_image = new multiLayerObject();
			_image.addLayers(bitmaps);
			_backSize = new Point(_image.width, _image.height);
//			size = _backSize;
			
			if (rescale) {
				var c: Point = new Point(_backSize.x/2, _backSize.y/2);
				bitmaps.forEach(function(bitmap: Bitmap, i: int, arr: Array): void {
					var mat		: Matrix3D 	= bitmap.transform.matrix3D.clone();
					var scale	: Number 	= 1 + bitmap.z/clt.transform.perspectiveProjection.focalLength;
					mat.appendTranslation(-c.x, -c.y, 0);
					mat.appendScale(scale, scale, 1);
					mat.appendTranslation(c.x, c.y, 0);
					bitmap.transform.matrix3D = mat;
				});
			}
			
			addChild(_image);
/*			
			_image.x = (width - _image.width) / 2; 
			_image.y = (height - _image.height) / 2;
*/			
			_image.mask = _mask;
			updateImageBounds();
		}
		
		override protected function doSetPosition(a_x:Number, a_y:Number):void {
			super.doSetPosition(a_x, a_y);
			updatePP();
		}
		
		public function get image(): multiLayerObject {
			return _image;
		}
		
		protected function updateImageBounds(): void {
/*			var rect: Rectangle = getInner();
			_image.scaleX = rect.width / _backSize.x;
			_image.scaleY = rect.height / _backSize.y;
			
			rect.inflate(-size.x * 0.02, -size.y * 0.02);*/
			//_mask.setRect(rect);
			
			
			var rect: Rectangle = getInner();
/*			
			_image.scaleX = rect.width/_image.width;
			_image.scaleY = rect.height/_image.height;
*/			
			rect.inflate(-size.x * 0.02, -size.y * 0.02);
			_mask.setRect(rect);
			updatePP();
		}
		
		override protected function updateBounds():uif_component {
			super.updateBounds();
			if (_image) updateImageBounds();
			return this;
		}
		
		protected function updatePP(): void {
			var rect	: Rectangle = getRect(clt);
			var pp		: PerspectiveProjection = new PerspectiveProjection()
			pp.projectionCenter = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
			pp.fieldOfView = 30;
			clt.transform.perspectiveProjection = pp;
		}
		
		public function get bytes(): ByteArray {
			return uif_MLPEncoder.encode(_image.layer3D);
		}
		
		override public function dispose():void {
			_image.dispose();
			super.dispose();
		}
	}
	
	
}